<template>
  <!-- 分配权限的弹层 -->
  <el-dialog
    title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
    :visible="showAssignDialog"
    @close="closeAssignDialog"
  >
    <!-- 渲染权限点列表 -->
    <el-tree
      ref="permTree"
      :data="permissionList"
      default-expand-all
      show-checkbox
      check-strictly
      node-key="id"
      :props="{ label: 'name' }"
    ></el-tree>

    <!-- 权限点数据展示 -->
    <template #footer>
      <el-button @click="closeAssignDialog">取消</el-button>
      <el-button type="primary" @click="assignPerms">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
// 导入权限点列表接口
import { getPermissionList } from '@/api/permission'
// 导入获取某个角色下权限点数据
import { getRoleDetailById, assignPerm } from '@/api/setting'
import { formatTreeData } from '@/utils'
export default {
  props: {
    showAssignDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 权限点列表
      permissionList: [],
      roleId: ''
    }
  },
  created () {
    this.getPermissionList()
  },
  methods: {
    closeAssignDialog () {
      this.$emit('update:showAssignDialog', false)
    },
    // 获取某个角色下权限点数据
    async getRolePerms (id) {
      // 存储角色ID
      this.roleId = id
      const { permIds } = await getRoleDetailById(id)
      this.$refs.permTree.setCheckedKeys(permIds)
    },
    // 获取权限点列表
    async getPermissionList () {
      const list = await getPermissionList()
      this.permissionList = formatTreeData(list)
    },
    // 分配权限点
    async assignPerms () {
      await assignPerm(
        {
          id: this.roleId,
          permIds: this.$refs.permTree.getCheckedKeys()
        }
      )
      this.$message.success('角色权限点分配成功')
      this.$emit('update:showAssignDialog', false)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
